package mobi.yuanshen.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import mobi.yuanshen.common.Exception.UserException;
import mobi.yuanshen.common.Exception.UserNotExistException;
import mobi.yuanshen.common.constants.ContexConstance;
import mobi.yuanshen.common.constants.ExceptionConstants;
import mobi.yuanshen.domain.dto.AddAdminDTO;
import mobi.yuanshen.domain.dto.UserLoginDTO;
import mobi.yuanshen.domain.entity.Admin;
import mobi.yuanshen.service.AdminService;
import mobi.yuanshen.mapper.AdminMapper;
import mobi.yuanshen.common.utils.SaltCodeUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 23207
* @description 针对表【admin(管理员用户表)】的数据库操作Service实现
* @createDate 2025-10-15 13:55:46
*/
@Service
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    private final AdminMapper adminMapper;

    private final StringRedisTemplate redisTemplate;

    @Override
    public List<String> getPermissionList(Long userId) {
        return adminMapper.getPermissionList(userId);
    }

    @Override
    public List<String> getRoleList(Long userId) {
        return adminMapper.getRoleList(userId);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     */
    @Override
    public String login(UserLoginDTO userLoginDTO) {
        String redisPicCode = redisTemplate.opsForValue().get(ContexConstance.REDIS_KEY_PIC_CODE + userLoginDTO.getPicCodeKey());
        if (redisPicCode == null) {
            throw new UserException("验证码无效");
        }
        if (!redisPicCode.equalsIgnoreCase(userLoginDTO.getPicCode())) {
            throw new UserException("验证码错误");
        }
        Admin admin = this.lambdaQuery()
                .eq(Admin::getUsername, userLoginDTO.getUsername())
                .or()
                .eq(Admin::getPhone, userLoginDTO.getUsername())
                .one();
        if (admin == null) {
            throw new UserNotExistException(ExceptionConstants.USER_CHECK_EXCEPTION);
        }
        String md5Password = SaltCodeUtils.getMdtPassword(userLoginDTO.getPassword(), admin.getPhone(), admin.getSalt());
        if (!admin.getPassword().equals(md5Password)) {
            throw new UserException(ExceptionConstants.USER_CHECK_EXCEPTION);
        }
        StpUtil.login(admin.getId());
        return StpUtil.getTokenInfo().getTokenValue();
    }

    /**
     * 添加管理员
     * @param addAdminDTO
     */
    @Override
    public void addAdmin(AddAdminDTO addAdminDTO) {
        Admin admin = new Admin();
        BeanUtil.copyProperties(addAdminDTO, admin);
        String saltCode = SaltCodeUtils.generateSaltCode(8);
        admin.setSalt(saltCode);
        admin.setPassword(SaltCodeUtils
                .getMdtPassword(addAdminDTO.getPassword(),
                        addAdminDTO.getPhone(),
                        saltCode));
        this.saveOrUpdate(admin);
    }
}




